package top;

/**
 * @author chenyw
 * @date 2022/6/18 9:49
 * @description     不使用额外空间删除有序数组的重复元素，返回删除后的长度
 *                  双指针，交换
 */
public class Top26removeDuplicates {
    public int removeDuplicates(int[] nums) {
        if(nums == null || nums.length == 0) {
            return 0;
        }
        int count = 1;
        int p = 0;
        int same = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == same) {
                continue;
            }
            p++;
            same = nums[i];
            int tmp = nums[i];
            nums[i] = nums[p];
            nums[p] = tmp;
            count++;
        }
        return count;
    }
}
